package org.ruiyichen.guizhou.agr.base;

import com.aspsine.swipetoloadlayout.OnLoadMoreListener;
import com.aspsine.swipetoloadlayout.OnRefreshListener;
import com.aspsine.swipetoloadlayout.SwipeToLoadLayout;
import org.ruiyichen.guizhou.agr.config.Constant;

/**
 * 带刷新的基础Activity
 */
public abstract class RefreshBaseActivity extends BaseActivity implements OnLoadMoreListener, OnRefreshListener {

    protected SwipeToLoadLayout bSwipeToLoadLayout;

    /**
     * 刷新类型
     */
    protected int bRefreshType = Constant.RefreshRelated.REFRESH;

    /**
     * 当前页数
     */
    protected int bCurPage = 1;

    protected void getData(int type) {
        if (type == Constant.RefreshRelated.REFRESH) {
            bCurPage = 1;
            getRefreshData();
        } else {
            bCurPage++;
            getMoreData();
        }
        bRefreshType = type;
    }

    protected void getData() {
        getData(Constant.RefreshRelated.REFRESH);
    }

    /**
     * 刷新
     */
    protected abstract void getRefreshData();

    /**
     * 获取更多
     */
    protected abstract void getMoreData();

    @Override
    protected void onPostHandle(int requestType, Object response, boolean error, int errorCode, Object obj1, Object obj2) {
        super.onPostHandle(requestType, response, error, errorCode, obj1, obj2);
        if (bSwipeToLoadLayout != null) {
            if (bRefreshType == Constant.RefreshRelated.REFRESH) {
                bSwipeToLoadLayout.setRefreshing(false);
            } else {
                bSwipeToLoadLayout.setLoadingMore(false);
            }
        }
    }

    @Override
    public void onLoadMore() {
        getData(Constant.RefreshRelated.LOADMORE);
    }

    @Override
    public void onRefresh() {
        getData(Constant.RefreshRelated.REFRESH);
    }
}
